using VOL.demo.IRepositories;
using VOL.demo.IServices;
using VOL.Core.BaseProvider;
using VOL.Entity.DomainModels;
using VOL.Core.Utilities;
using System.Linq;
using VOL.Core.Extensions;

namespace VOL.demo.Services
{
    public partial class InventoryModelService
    {
        /// <summary>
        /// 根据货品ID获取库存信息
        /// </summary>
        /// <param name="goodsId">货品ID</param>
        /// <returns></returns>
        public List<InventoryModel> GetInventoryByGoodsId(long goodsId)
        {
            return repository.Find(x => x.GoodsId == goodsId && x.IsDel == 0);
        }

        /// <summary>
        /// 根据仓库ID获取库存信息
        /// </summary>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns></returns>
        public List<InventoryModel> GetInventoryByWarehouseId(long warehouseId)
        {
            return repository.Find(x => x.WarehouseId == warehouseId && x.IsDel == 0);
        }

        /// <summary>
        /// 验证库存上下限
        /// </summary>
        /// <param name="upperLimit">库存上限</param>
        /// <param name="lowerLimit">库存下限</param>
        /// <returns></returns>
        public WebResponseContent ValidateInventoryLimits(int? upperLimit, int? lowerLimit)
        {
            if (upperLimit.HasValue && lowerLimit.HasValue)
            {
                if (upperLimit.Value <= lowerLimit.Value)
                {
                    return new WebResponseContent().Error("库存上限应大于库存下限！");
                }
            }
            return new WebResponseContent().OK();
        }

        /// <summary>
        /// 批量设置库存上下限
        /// </summary>
        /// <param name="inventoryIds">库存ID列表</param>
        /// <param name="upperLimit">库存上限</param>
        /// <param name="lowerLimit">库存下限</param>
        /// <returns></returns>
        public WebResponseContent BatchSetInventoryLimits(List<long> inventoryIds, int? upperLimit, int? lowerLimit)
        {
            var validation = ValidateInventoryLimits(upperLimit, lowerLimit);
            if (!validation.Status)
            {
                return validation;
            }

            return repository.DbContextBeginTransaction(() =>
            {
                try
                {
                    var inventories = repository.Find(x => inventoryIds.Contains(x.Id) && x.IsDel == 0);
                    foreach (var inventory in inventories)
                    {
                        if (upperLimit.HasValue)
                            inventory.UpperLimit = upperLimit.Value;
                        if (lowerLimit.HasValue)
                            inventory.LowerLimit = lowerLimit.Value;
                        inventory.SetModifyDefaultVal();
                    }
                    repository.UpdateRange(inventories, true);
                    return new WebResponseContent().OK("批量设置库存上下限成功");
                }
                catch (Exception ex)
                {
                    return new WebResponseContent().Error($"批量设置库存上下限失败：{ex.Message}");
                }
            });
        }

        /// <summary>
        /// 保存库存信息
        /// </summary>
        /// <param name="inventory">库存信息</param>
        /// <returns></returns>
        public WebResponseContent SaveInventory(InventoryModel inventory)
        {
            var validation = ValidateInventoryLimits(inventory.UpperLimit, inventory.LowerLimit);
            if (!validation.Status)
            {
                return validation;
            }

            return repository.DbContextBeginTransaction(() =>
            {
                try
                {
                    inventory.SetCreateDefaultVal();
                    repository.AddWithSetIdentity(inventory);
                    return new WebResponseContent().OK("库存信息保存成功", inventory);
                }
                catch (Exception ex)
                {
                    return new WebResponseContent().Error($"库存信息保存失败：{ex.Message}");
                }
            });
        }
    }
}
